﻿namespace QLPhongKham.BaseClass.VienPhi
{
    using ConfigDatabase.Class;
    using Npgsql;
    using NpgsqlTypes;
    using System;
    using System.Data;

    public class dtovpThanhToanVPCT
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _connect;
        private decimal _dedongia;
        private long _deid;
        private decimal _deidchidinh;
        private decimal _deidnoidung;
        private long _deidttvp;
        private int _desoluongthu;
        private decimal _dethucthu;
        private int _iidnguoichidinh;
        private int _iloainoidung;
        private string _sdvt;
        private string _smanoidung;
        private string _snguoichidinh;
        private string _sSchema;
        private string _stennoidung;

        public dtovpThanhToanVPCT()
        {
            this.KhoiTao();
            this._condata = new ConnectDatabase();
            this._sSchema = this._condata.Schema;
        }

        public dtovpThanhToanVPCT(ConnectDatabase conn)
        {
            this.KhoiTao();
            this._condata = conn;
            this._sSchema = this._condata.Schema;
        }

        public bool Delete()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Delete(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtovpThanhToanVPCT.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Delete(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { "delete from ", this._sSchema, ".", tableName, " where 1=1  and ", columnID, " = ", this._deid }), connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtovpThanhToanVPCT.cs");
                return false;
            }
        }

        public int GetIDInsert()
        {
            int iDInsert;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                iDInsert = this.GetIDInsert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert()", "dtovpThanhToanVPCT.cs");
                iDInsert = 1;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return iDInsert;
        }

        public int GetIDInsert(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select max(id) as idmax  from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                int num = 0;
                try
                {
                    num = int.Parse(dataTable.Rows[0][0].ToString());
                }
                catch
                {
                }
                return ++num;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert(NpgsqlConnection connect)", "dtovpThanhToanVPCT.cs");
                return 1;
            }
        }

        public bool Insert()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Insert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtovpThanhToanVPCT.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Insert(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "insert into ", this._sSchema, ".", tableName, "(", columnID, ",", columnIDTTVP, ",", columnIDNOIDUNG, ",", columnMANOIDUNG, ",", columnTENNOIDUNG, ",", columnDVT, 
                    ",", columnLOAINOIDUNG, ",", columnSOLUONGTHU, ",", columnDONGIA, ",", columnTHUCTHU, ",", columnIDCHIDINH, ",", columnNGUOICHIDINH, ",", columnIDNGUOICHIDINH, ") values(", this._deid, 
                    ",", this._deidttvp, ",", this._deidnoidung, ",'", this._smanoidung, "',:", columnTENNOIDUNG, ",:", columnDVT, ",", this._iloainoidung, ",:", columnSOLUONGTHU, ",:", columnDONGIA, 
                    ",:", columnTHUCTHU, ",", this._deidchidinh, ",:", columnNGUOICHIDINH, ",", this._iidnguoichidinh, ")"
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnTENNOIDUNG, NpgsqlDbType.Text, 200).Value = this._stennoidung;
                command.Parameters.Add(columnDVT, NpgsqlDbType.Text, 200).Value = this._sdvt;
                command.Parameters.Add(columnNGUOICHIDINH, NpgsqlDbType.Text, 200).Value = this._snguoichidinh;
                command.Parameters.Add(columnSOLUONGTHU, NpgsqlDbType.Numeric, 20).Value = this._desoluongthu;
                command.Parameters.Add(columnDONGIA, NpgsqlDbType.Numeric, 20).Value = this._dedongia;
                command.Parameters.Add(columnTHUCTHU, NpgsqlDbType.Numeric, 20).Value = this._dethucthu;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtovpThanhToanVPCT.cs");
                return false;
            }
        }

        private void KhoiTao()
        {
            this._deid = 0L;
            this._deidttvp = 0L;
            this._deidnoidung = 0M;
            this._smanoidung = "";
            this._stennoidung = "";
            this._sdvt = "";
            this._iloainoidung = 0;
            this._desoluongthu = 0;
            this._dedongia = 0M;
            this._dethucthu = 0M;
            this._deidchidinh = 0M;
            this._snguoichidinh = "";
            this._iidnguoichidinh = 0;
        }

        public void Load()
        {
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                this.Load(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load()", "dtovpThanhToanVPCT.cs");
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
        }

        public void Load(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(string.Concat(new object[] { 
                    " select  ", columnID, ",", columnIDTTVP, ",", columnIDNOIDUNG, ",", columnMANOIDUNG, ",", columnTENNOIDUNG, ",", columnDVT, ",", columnLOAINOIDUNG, ",", columnSOLUONGTHU, 
                    ",", columnDONGIA, ",", columnTHUCTHU, ",", columnIDCHIDINH, ",", columnNGUOICHIDINH, ",", columnIDNGUOICHIDINH, " from ", this._sSchema, ".", tableName, " where ", columnID, 
                    " = ", this._deid
                 }), connect).Fill(dataTable);
                this._deid = long.Parse(dataTable.Rows[0][columnID].ToString());
                this._deidttvp = long.Parse(dataTable.Rows[0][columnIDTTVP].ToString());
                this._deidnoidung = decimal.Parse(dataTable.Rows[0][columnIDNOIDUNG].ToString());
                this._smanoidung = dataTable.Rows[0][columnMANOIDUNG].ToString();
                this._stennoidung = dataTable.Rows[0][columnTENNOIDUNG].ToString();
                this._sdvt = dataTable.Rows[0][columnDVT].ToString();
                this._iloainoidung = int.Parse(dataTable.Rows[0][columnLOAINOIDUNG].ToString());
                this._desoluongthu = int.Parse(dataTable.Rows[0][columnSOLUONGTHU].ToString());
                this._dedongia = decimal.Parse(dataTable.Rows[0][columnDONGIA].ToString());
                this._dethucthu = decimal.Parse(dataTable.Rows[0][columnTHUCTHU].ToString());
                this._deidchidinh = decimal.Parse(dataTable.Rows[0][columnIDCHIDINH].ToString());
                this._snguoichidinh = dataTable.Rows[0][columnNGUOICHIDINH].ToString();
                this._iidnguoichidinh = int.Parse(dataTable.Rows[0][columnIDNGUOICHIDINH].ToString());
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load(NpgsqlConnection connect)", "dtovpThanhToanVPCT.cs");
            }
        }

        public DataTable LoadAll()
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "dtovpThanhToanVPCT.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnID + "," + columnIDTTVP + "," + columnIDNOIDUNG + "," + columnMANOIDUNG + "," + columnTENNOIDUNG + "," + columnDVT + "," + columnLOAINOIDUNG + "," + columnSOLUONGTHU + "," + columnDONGIA + "," + columnTHUCTHU + "," + columnIDCHIDINH + "," + columnNGUOICHIDINH + "," + columnIDNGUOICHIDINH + " from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                return dataTable;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect)", "dtovpThanhToanVPCT.cs");
                return null;
            }
        }

        public bool Update()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Update(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtovpThanhToanVPCT.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Update(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "update ", this._sSchema, ".", tableName, " set ", columnIDTTVP, " = ", this._deidttvp, ",", columnIDNOIDUNG, " = ", this._deidnoidung, ",", columnMANOIDUNG, " = '", this._smanoidung, 
                    "',", columnTENNOIDUNG, " = :", columnTENNOIDUNG, ",", columnDVT, " = :", columnDVT, ",", columnLOAINOIDUNG, " = ", this._iloainoidung, ",", columnSOLUONGTHU, " = :", columnSOLUONGTHU, 
                    ",", columnDONGIA, " = :", columnDONGIA, ",", columnTHUCTHU, " = :", columnTHUCTHU, ",", columnIDCHIDINH, " = ", this._deidchidinh, ",", columnNGUOICHIDINH, " = :", columnNGUOICHIDINH, 
                    ",", columnIDNGUOICHIDINH, " = ", this._iidnguoichidinh, " where ", columnID, " = ", this._deid
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnTENNOIDUNG, NpgsqlDbType.Text, 200).Value = this._stennoidung;
                command.Parameters.Add(columnDVT, NpgsqlDbType.Text, 200).Value = this._sdvt;
                command.Parameters.Add(columnNGUOICHIDINH, NpgsqlDbType.Text, 200).Value = this._snguoichidinh;
                command.Parameters.Add(columnSOLUONGTHU, NpgsqlDbType.Numeric, 20).Value = this._desoluongthu;
                command.Parameters.Add(columnDONGIA, NpgsqlDbType.Numeric, 20).Value = this._dedongia;
                command.Parameters.Add(columnTHUCTHU, NpgsqlDbType.Numeric, 20).Value = this._dethucthu;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtovpThanhToanVPCT.cs");
                return false;
            }
        }

        public static string columnDONGIA
        {
            get
            {
                return "dongia";
            }
        }

        public static string columnDVT
        {
            get
            {
                return "dvt";
            }
        }

        public static string columnID
        {
            get
            {
                return "id";
            }
        }

        public static string columnIDCHIDINH
        {
            get
            {
                return "idchidinh";
            }
        }

        public static string columnIDNGUOICHIDINH
        {
            get
            {
                return "idnguoichidinh";
            }
        }

        public static string columnIDNOIDUNG
        {
            get
            {
                return "idnoidung";
            }
        }

        public static string columnIDTTVP
        {
            get
            {
                return "idttvp";
            }
        }

        public static string columnLOAINOIDUNG
        {
            get
            {
                return "loainoidung";
            }
        }

        public static string columnMANOIDUNG
        {
            get
            {
                return "manoidung";
            }
        }

        public static string columnNGUOICHIDINH
        {
            get
            {
                return "nguoichidinh";
            }
        }

        public static string columnSOLUONGTHU
        {
            get
            {
                return "soluongthu";
            }
        }

        public static string columnTENNOIDUNG
        {
            get
            {
                return "tennoidung";
            }
        }

        public static string columnTHUCTHU
        {
            get
            {
                return "thucthu";
            }
        }

        public decimal pDONGIA
        {
            get
            {
                return this._dedongia;
            }
            set
            {
                this._dedongia = value;
            }
        }

        public string pDVT
        {
            get
            {
                return this._sdvt;
            }
            set
            {
                this._sdvt = value;
            }
        }

        public long pID
        {
            get
            {
                return this._deid;
            }
            set
            {
                this._deid = value;
            }
        }

        public decimal pIDCHIDINH
        {
            get
            {
                return this._deidchidinh;
            }
            set
            {
                this._deidchidinh = value;
            }
        }

        public int pIDNGUOICHIDINH
        {
            get
            {
                return this._iidnguoichidinh;
            }
            set
            {
                this._iidnguoichidinh = value;
            }
        }

        public decimal pIDNOIDUNG
        {
            get
            {
                return this._deidnoidung;
            }
            set
            {
                this._deidnoidung = value;
            }
        }

        public long pIDTTVP
        {
            get
            {
                return this._deidttvp;
            }
            set
            {
                this._deidttvp = value;
            }
        }

        public int pLOAINOIDUNG
        {
            get
            {
                return this._iloainoidung;
            }
            set
            {
                this._iloainoidung = value;
            }
        }

        public string pMANOIDUNG
        {
            get
            {
                return this._smanoidung;
            }
            set
            {
                this._smanoidung = value;
            }
        }

        public string pNGUOICHIDINH
        {
            get
            {
                return this._snguoichidinh;
            }
            set
            {
                this._snguoichidinh = value;
            }
        }

        public int pSOLUONGTHU
        {
            get
            {
                return this._desoluongthu;
            }
            set
            {
                this._desoluongthu = value;
            }
        }

        public string pTENNOIDUNG
        {
            get
            {
                return this._stennoidung;
            }
            set
            {
                this._stennoidung = value;
            }
        }

        public decimal pTHUCTHU
        {
            get
            {
                return this._dethucthu;
            }
            set
            {
                this._dethucthu = value;
            }
        }

        public static string tableName
        {
            get
            {
                return "vp_ttvpct";
            }
        }
    }
}

